<?php
include "../config/koneksi.php";
include "../class/produk.php";
include "../class/kategori.php";
include "../class/toko.php";

if (ISSET($_GET["id_oleh2"])){
	$id_oleh2=$_GET["id_oleh2"];
	$produk_dao = new Produk_Dao();
	$produk=$produk_dao->get($id_oleh2);
}
$Kategori_Dao = new Kategori_Dao();
$kategori=$Kategori_Dao->get_all();
$Toko_Dao = new Toko_Dao();
$toko=$Toko_Dao->get_all();

if (ISSET($_POST["edit"]) && ISSET($_POST["Submit42"]) && $_POST['id_kategori']!="" && $_POST['nama_oleh2']!="" && $_POST['harga']!="" && $_POST['detil']!=""){

          $namafolder="../images/gambar_produk/";  //tempat menyimpan file
          $filename = basename ($_FILES["gambar"]["name"]);
          $file_upload = $namafolder . $filename;
          if (move_uploaded_file($_FILES["gambar"]["tmp_name"],$file_upload)){
		  
			$produk_dao = new Produk_Dao();
			$produk = new Produk();
			$produk->id_oleh2 = $_POST['id_oleh2'];
			$produk->id_kategori = $_POST['id_kategori'];
			$produk->id_toko = $_POST['id_toko'];
			$produk->nama_oleh2 = $_POST['nama_oleh2'];
			$produk->harga = $_POST['harga'];
			$produk->gambar = $filename;
			$produk->detil = $_POST['detil'];
			$produk_dao->edit($produk);
			header("Location:index.php?menu=view_produk");
}
}
?>
<html>
<head>
<title>Edit Produk</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<table width="100%" height="30" border="0" cellpadding="0" cellspacing="0">
  <tr >
    <td height="30"> 
      <div align="left"><font size="3" face="tahoma">Edit Produk</font><hr color=#CCCCFF></div></td>
    </tr>
  </table>
<form name="edit" method="post" action="edit_produk.php" enctype="multipart/form-data" >
<input type="hidden" name="edit">
<input type="hidden" name="id_oleh2" value="<?php echo $produk->id_oleh2;?>">
<input type="hidden" name="id_toko" value="<?php echo $produk->id_toko;?>">

  <table width="60%" border="0" align="center" bordercolor="#FF9900" bgcolor="#FFFFFF">
  
   <tr bordercolor="#FFFFFF"> 
      <td><font size="2" face="tahoma">Kategori</font></td>
      <td valign="top"><font size="2" face="tahoma">:</font></td>
      <td valign=top> <select name="id_kategori" id="select">
	   <?php
       foreach($kategori as $kategori1){
       $pilih="";
       if($produk->id_kategori == $kategori1->id_kategori){
       $pilih = "pilih";
	   }
       ?>
       <option value="<?php echo $kategori1->id_kategori;?>" <?php echo $pilih;?>><?php echo $kategori1->kategori;?></option>
       <?php }?>
       </select></td>
    </tr>	 
	 <tr bordercolor="#FFFFFF"> 
      <td><font size="2" face="tahoma">Nama Oleh-oleh</font></td>
      <td valign=top>:</td>
      <td valign=top> <input name="nama_oleh2" type="text" id="nama_oleh2" value='<?php echo $produk->nama_oleh2;?>'></td>
    </tr>
	 <tr bordercolor="#FFFFFF"> 
      <td><font size="2" face="tahoma">Harga</font></td>
      <td valign=top>:</td>
      <td valign=top> <input name="harga" type="text" id="harga" value='<?php echo $produk->harga;?>'></td>
    </tr>
    <tr bordercolor=#FFFFFF> 
      <td height=107 valign=top><font size=2 face=tahoma>Gambar</font></font></td>
      <td valign=top><font size=2 face=tahoma>:</font></td>
      <td colspan=2 valign=top><font size=2 face=tahoma><?php echo "<image src=\"../images/gambar_produk/$produk->gambar\" width=150 height=150>";?></font></td>
    </tr>
	 <tr bordercolor="#FFFFFF"> 
      <td><font size="2" face="tahoma">Ganti Gambar / Isi Gambar yang lama</font></td>
      <td valign=top>:</td>
      <td valign=top><input name="gambar" type="file" id="gambar" value='<?php echo $produk->gambar;?>'></td>
    </tr>
	 <tr bordercolor="#FFFFFF"> 
      <td><font size="2" face="tahoma">Detil</font></td>
      <td valign=center>:</td>
      <td valign=top> <textarea name=detil cols=30 rows=5> <?php echo $produk->detil?></textarea></td>
    </tr>
    <tr bordercolor="#FFFFFF"> 
      <td height="21" colspan="2"></td><td><font size="2" face="tahoma"> 
          <input type="submit" name="Submit42" value="Simpan">
		  <button type ="button" name="fulang2" value="Batal" class="btn btn-success" onClick="javascript:history.back()">Batal
          </font>
        </td>
    </tr>
  </table>
</form>
</html>